﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Hydra.Shell.Interfaces;
using Utils;
using Hydra.Shell.Enumerations;
using System.Diagnostics;

namespace Hydra.Shell.Implementations
{
    public class HydraPackageWrapper : HydraObjectWrapper
    {
        private WeakReference<IHydraPackage> weakReference;
        private IHydraPackage strongReference;
        private Guid packageGuid;
        public Type PackageType { get; private set; }
        public static EventHandler ObjectDisposed { get; set; }
        public bool IsDisposed { get; private set; }

        static HydraPackageWrapper()
        {
            _HydraServiceBase.Disposed += (sender, e) =>
            {
                if (ObjectDisposed != null)
                {
                    ObjectDisposed(sender, e);
                }
            };

        }

        public HydraPackageWrapper(IHydraPackage package)
        {
            packageGuid = package.PackageGuid;

            HydraPackageWrapper.ObjectDisposed += (sender, e) =>
            {
                if (sender == weakReference.Object)
                {
                    this.IsDisposed = true;
                }
            };

            PackageType = package.GetType();

            var lifetimeType = GetLifetimeType(package);

            switch (lifetimeType)
            {
                case ServiceLifetimeType.NormalGarbageCollected:
                    weakReference = new WeakReference<IHydraPackage>(package);
                    break;
                case ServiceLifetimeType.ShellInstance:
                    strongReference = package;
                    break;
                case ServiceLifetimeType.PerThread:
                case ServiceLifetimeType.ExternallyControlled:

                    Debugger.Break(); // TODO
                    break;
            }
        }

        public override int GetHashCode()
        {
            return this.Package.GetHashCode();
        }

        public IHydraPackage Package
        {
            get
            {
                if (weakReference != null)
                {
                    if (this.IsDisposed)
                    {
                        throw new ObjectDisposedException(string.Format("HydraPackage {{{0}}}}", packageGuid));
                    }

                    return weakReference.Object;
                }
                else
                {
                    return strongReference;
                }
            }
        }

        public Guid PackageGuid
        {
            get
            {
                if (weakReference != null && this.IsDisposed)
                {
                    throw new ObjectDisposedException(string.Format("HydraPackage {{{0}}}}", packageGuid));
                }

                return Package.PackageGuid;
            }
        }
    }
}
